package com.fzl.lotterysystem.common.pojo;

import com.fzl.lotterysystem.common.errorcode.ErrorCode;
import com.fzl.lotterysystem.common.errorcode.GlobalErrorCodeConstants;
import lombok.Data;
import org.springframework.util.Assert;

import java.io.Serializable;

@Data
public class CommonResult<T> implements Serializable {
//    返回错误码
    private Integer code;
//    返回数据
    private T data;
//    错误信息
    private String msg;

    public static <T> CommonResult<T> success(T data) {
        CommonResult<T> result = new CommonResult<T>();
        result.code= GlobalErrorCodeConstants.SUCCESS.getCode();
        result.data = data;
        result.msg="访问成功";
        return result;
    }
    public static <T> CommonResult<T> error(Integer code, String msg) {
        Assert.isTrue(!GlobalErrorCodeConstants.SUCCESS.getCode().equals(code),"不是错误异常");
        CommonResult<T> result = new CommonResult<T>();
        result.code=code;
        result.msg=msg;
        return result;
    }

    public static <T> CommonResult<T> error(ErrorCode errorCode) {
        return error(errorCode.getCode(), errorCode.getMessage());
    }
}
